<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Veh_Insurance_Create extends Page
{
       public $Label12 = null;
       public $hfError = null;
       public $lblpol = null;
       public $lblto = null;
       public $lblper = null;
       public $lblrem = null;
       public $JSValidate = null;
       public $cmbIntermed = null;
       public $cmbInsuranceComp = null;
       public $memoRemarks = null;
       public $Label1 = null;
       public $Label5 = null;
       public $edDue = null;
       public $Label4 = null;
       public $imgBack = null;
       public $hiddenId = null;
       public $Image2 = null;
       public $btnSubmit = null;
       public $btnCancel = null;
       public $Label76 = null;
       public $edtotAmt = null;
       public $edtotPrice = null;
       public $edPremium2 = null;
       public $edOthersSum2 = null;
       public $edCat2 = null;
       public $edCat1 = null;
       public $edOthersSum1 = null;
       public $edPremium1 = null;
       public $Label79 = null;
       public $Label73 = null;
       public $Label80 = null;
       public $Label72 = null;
       public $Label71 = null;
       public $edamtELPD = null;
       public $edPrELPD = null;
       public $edPrELBI = null;
       public $edamtELBI = null;
       public $Label65 = null;
       public $Label41 = null;
       public $edPrOD = null;
       public $edamtOD = null;
       public $edPrCTPL = null;
       public $edamtCTPL = null;
       public $Label75 = null;
       public $Label74 = null;
       public $Label40 = null;
       public $Label39 = null;
       public $jtdaterenew = null;
       public $Label78 = null;
       public $jtdatecover = null;
       public $Label77 = null;
       public $edAgentInsurance = null;
       public $lblAdress = null;
       public $edCompInsurance = null;
       public $Label42 = null;
       public $edPolicyNum = null;
       public $lblPolicy = null;
       function edDueJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edDue','lblrem');
       <?php

       }

       function edPolicyNumJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edPolicyNum','lblpol');
       <?php

       }

       function edDueJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       var numero = document.getElementById("edDue").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edDue").value = numero;
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('veh_insurance_list.php?veh='.$this->hiddenId->getValue());

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }


       function btnSubmitClick($sender, $params)
       {

       $this->validateRequired($this->jtdaterenew->Text, 'lblto');
       $this->validateRequired($this->jtdatecover->Text, 'lblper');
       $this->validateRequired($this->edPolicyNum->Text, 'lblpol');
       $this->validateRequired($this->edDue->Text, 'lblrem');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $tot_coverage = $this->edtotAmt->Text;
       $tot_price = $this->edtotPrice->Text;
       $tot_c = $this->commafy($tot_coverage);
       $tot_p = $this->commafy($tot_price);

       $vehicle_id = $this->hiddenId->getValue();

       mysql_query("insert into veh_insurance_dynamic_tbl(vehicle_id,
       company_id,
       policy_num,
       company,
       agent,
       category1,
       category2,
       ctpl_amt,
       od_amt,
       elbi_amt,
       elpd_amt,
       other1_amt,
       other2_amt,
       tot_amt,
       ctpl_price,
       od_price,
       elbi_price,
       elpd_price,
       premium1_amt,
       premium2_amt,
       tot_price,
       cover_date,
       renewal_date,
       due_days,
       remarks)

       values('" . $this->hiddenId->getValue() . "',
       '".$_SESSION['company_id']."',
       '" . $this->edPolicyNum->Text . "',
       '" . $this->cmbInsuranceComp->getItemIndex() . "',
       '" . $this->cmbIntermed->getItemIndex() . "',
       '" . $this->edCat1->Text . "',
       '" . $this->edCat2->Text . "',
       '" . $this->edamtCTPL->Text . "',
       '" . $this->edamtOD->Text . "',
       '" . $this->edamtELBI->Text . "',
       '" . $this->edamtELPD->Text . "',
       '" . $this->edOthersSum1->Text . "',
       '" . $this->edOthersSum2->Text . "',
       '" . $tot_coverage . "',
       '" . $this->edPrCTPL->Text . "',
       '" . $this->edPrOD->Text . "',
       '" . $this->edPrELBI->Text . "',
       '" . $this->edPrELPD->Text . "',
       '" . $this->edPremium1->Text . "',
       '" . $this->edPremium2->Text . "',
       '" . $tot_price . "',
       '" . $this->jtdatecover->Text . "',
       '" . $this->jtdaterenew->Text . "',
       '".$this->edDue->Text."',
       '".$this->memoRemarks->Text."')")or die("INSERT INSURANCE - " . mysql_error());


       $result_ins_id = mysql_query("SELECT LAST_INSERT_ID(insurance_id) AS insid FROM veh_insurance_dynamic_tbl ORDER BY insurance_id DESC LIMIT 0,1") or die("SELECT INS ID - ".mysql_error());
       $row_ins_id = mysql_fetch_array($result_ins_id);
       $ins_id = $row_ins_id['insid'];


       mysql_query("insert into veh_reminder_tbl (company_id,
       vehicle_id,
       insurance_id,
       schedule_by,
       due_date,
       rem_by,
       rem_time,
       completion_window,
       type,
       description,
       vendor,
       status2)

       values('".$_SESSION['company_id']."',
       '".$this->hiddenId->getValue()."',
       '".$ins_id."',
       'Date',
       '".$this->jtdaterenew->Text."',
       'Days',
       '".$this->edDue->Text."',
       'Insurance',
       'Insurance',
       '".$this->memoRemarks->Text."',
       '".$this->cmbInsuranceComp->getItemIndex()."',
       'Pending')")or die("INSERT REMINDER - " . mysql_error());


       echo "<script type='text/javascript'>
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.location = 'veh_insurance_create.php?veh=".$vehicle_id."';
       }
       else
       {
       window.location = 'veh_insurance_list.php?veh=".$vehicle_id."';
       }
       </script>";
       }

       }


       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }





       function Veh_Insurance_CreateCreate($sender, $params)
       {

        $id = $_GET['veh'];
        if($id!=''||$id!=null)
        $this->hiddenId->setValue($id);

        $this->hfError->setValue('0');

        $veh_id = $this->hiddenId->getValue();

        $this->edPolicyNum->Text = '';
        $this->edCompInsurance->Text = '';
        $this->edAgentInsurance->Text = '';
        $this->edamtCTPL->Text = '';
        $this->edamtOD->Text = '';
        $this->edamtELBI->Text = '';
        $this->edamtELPD->Text = '';
        $this->edCat1->Text = '';
        $this->edCat2->Text = '';
        $this->edOthersSum1->Text = '';
        $this->edOthersSum2->Text = '';
        $this->edPremium1->Text = '';
        $this->edPremium2->Text = '';
        $this->edtotAmt->Text = '';
        $this->edPrCTPL->Text = '';
        $this->edPrOD->Text = '';
        $this->edPrELBI->Text = '';
        $this->edPrELPD->Text = '';
        $this->edtotPrice->Text = '';
        $this->jtdatecover->Text = '';
        $this->jtdaterenew->Text = '';
        $this->memoRemarks->Lines = array();

        $this->lblpol->Caption = '';
        $this->lblto->Caption = '';
        $this->lblper->Caption = '';
        $this->lblrem->Caption = '';

       $this->cmbInsuranceComp->Clear();
       $this->cmbIntermed->Clear();
       $this->cmbInsuranceComp->ItemIndex = '--Select--';
       $this->cmbIntermed->ItemIndex = '--Select--';
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbInsuranceComp->AddItem('--Select--', null, '--Select--');
            $this->cmbIntermed->AddItem('--Select--', null, '--Select--');
            $this->cmbInsuranceComp->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
            $this->cmbIntermed->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }



        $this->imgBack->setLink('veh_insurance_list.php?veh='.$veh_id);
        //$this->imgBack->setLink('vehicle_list.php?company_id='.$_SESSION['company_id']);


       }

       function edOthersSum2JSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edOthersSum2").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edOthersSum2").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edOthersSum1JSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edOthersSum1").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edOthersSum1").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edamtELPDJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edamtELPD").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edamtELPD").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edamtELBIJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edamtELBI").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edamtELBI").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edamtODJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edamtOD").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edamtOD").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edamtCTPLJSKeyUp($sender, $params)
       {
       wwww
       ?>
       var numero = document.getElementById("edamtCTPL").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edamtCTPL").value = numero;

       document.Veh_Insurance_Create.edtotAmt.value = Number(document.Veh_Insurance_Create.edamtCTPL.value) +
   Number(document.Veh_Insurance_Create.edamtOD.value) +
   Number(document.Veh_Insurance_Create.edamtELBI.value) +
   Number(document.Veh_Insurance_Create.edamtELPD.value) +
   Number(document.Veh_Insurance_Create.edOthersSum1.value) +
   Number(document.Veh_Insurance_Create.edOthersSum2.value);

       <?php

       }

       function edPremium2JSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPremium2").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPremium2").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

       function edPremium1JSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPremium1").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPremium1").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

       function edPrELPDJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPrELPD").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrELPD").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

       function edPrELBIJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPrELBI").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrELBI").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

       function edPrODJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPrOD").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrOD").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

       function edPrCTPLJSKeyUp($sender, $params)
       {

       ?>
       var numero = document.getElementById("edPrCTPL").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrCTPL").value = numero;

       document.Veh_Insurance_Create.edtotPrice.value = Number(document.Veh_Insurance_Create.edPrCTPL.value) +
   Number(document.Veh_Insurance_Create.edPrOD.value) +
   Number(document.Veh_Insurance_Create.edPrELBI.value) +
   Number(document.Veh_Insurance_Create.edPrELPD.value) +
   Number(document.Veh_Insurance_Create.edPremium1.value) +
   Number(document.Veh_Insurance_Create.edPremium2.value);

       <?php

       }

}

global $application;

global $Veh_Insurance_Create;

//Creates the form
$Veh_Insurance_Create=new Veh_Insurance_Create($application);

//Read from resource file
$Veh_Insurance_Create->loadResource(__FILE__);

//Shows the form
$Veh_Insurance_Create->show();

?>